package entity

import (
	"time"

	"github.com/cihub/seelog"
	"gorm.io/gorm"
)

// CommonEntity 数据库通用entity
type CommonEntity struct {
	ID        string `gorm:"primaryKey;size:50"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`
}

// CheckDbRes 检测db返回结果是否正常
func CheckDbRes(db *gorm.DB) bool {
	err := db.Error
	if nil != err {
		_ = seelog.Errorf("db返回结果异常:%s", err)
	}
	return nil == err
}

// PageEntity 分页entity
type PageEntity struct {
	CurrentPage  int
	PageSize     int
	PagesCount   int
	RecordsCount int
}

// NewPageEntity 创建分页entity
func NewPageEntity(page int, size int, totalTmp int64) *PageEntity {
	total := int(totalTmp)
	left := total % size
	pages := total / size
	if left > 0 {
		pages++
	}
	return &PageEntity{
		CurrentPage:  page,
		PageSize:     size,
		PagesCount:   pages,
		RecordsCount: total,
	}
}
